home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFskyedit - Star Fighter 3000 sky colours editor
- * Insertion dbox
- * Copyright (C) 2001 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- /* ANSI library files */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- /* RISC OS library files */
- #include "wimp.h"
- #include "toolbox.h"
- #include "event.h"
- #include "wimplib.h"
- #include "window.h"
- #include "gadgets.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "Macros.h"
- #include "Pal256.h"
-
- /* Local headers */
- #include "Utils.h"
- #include "EditSky.h"
- #include "Back-end.h"
- #include "Insert.h"
- #include "Main.h"
-
- /* Gadgets */
- #define INSERT_NUMTOINS 0x03
- #define INSERT_RADPLAIN 0x18
- #define INSERT_RADGRAD 0x19
-
- #define INSERT_PLAINLAB 0x15
- #define INSERT_PLAINCOL 0x13
- #define INSERT_PLAINPOPUP 0x14
-
- #define INSERT_ENDLAB 0x1c
- #define INSERT_ENDCOL 0x1a
- #define INSERT_ENDPOPUP 0x1b
- #define INSERT_INCEND 0x16
-
- #define INSERT_STARTLAB 0x0c
- #define INSERT_STARTCOL 0x06
- #define INSERT_STARTPOPUP 0x1d
- #define INSERT_INCSTART 0x17
-
- #define INSERT_CANCEL 0x00
- #define INSERT_GO 0x01
-
- ObjectId Insert_sharedid = NULL_ObjectId;
- static char fill_colour, reset_colour, start_colour, end_colour;
- static int number;
- static ComponentId radio_sel;
- static bool have_caret;
-
- /* ----------------------------------------------------------------------- */
- /* Function prototypes */
-
- static ToolboxEventHandler _Insert_buttonhandler, _Insert_colourshandler, _Insert_popuphandler, _Insert_showhandler, _Insert_radiohandler;
- static void update_grad_fill(bool sel), update_plain_fill(bool sel);
- static _kernel_oserror *reset_start_end(ViewData *view_data);
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- void Insert_initialise(ObjectId object)
- {
- /* Record ID */
- Insert_sharedid = object;
-
- /* Install handlers */
- EF(event_register_toolbox_handler(object, Window_AboutToBeShown, _Insert_showhandler, NULL));
- EF(event_register_toolbox_handler(object, ActionButton_Selected, _Insert_buttonhandler, NULL));
- EF(event_register_toolbox_handler(pal256_sharedid, Pal256_ColourSelected, _Insert_colourshandler, NULL));
- EF(event_register_toolbox_handler(object, PopUp_AboutToBeShown, _Insert_popuphandler, NULL));
- EF(event_register_toolbox_handler(object, RadioButton_StateChanged, _Insert_radiohandler, NULL));
- EF(event_register_toolbox_handler(object, Window_HasBeenHidden, hand_back_caret, &have_caret));
- EF(event_register_wimp_handler(object, -1, watch_caret, &have_caret));
-
- /* Store initial state of dbox */
- EF(numberrange_get_value(0, object, INSERT_NUMTOINS, &number));
- EF(radiobutton_get_state(0, object, INSERT_RADPLAIN, NULL, &radio_sel));
-
- reset_colour = 0; /* black */
- have_caret = false;
- }
-
- /* ----------------------------------------------------------------------- */
- /* Private functions */
-
- static int _Insert_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Set up dialogue window */
- ViewData *view_data;
-
- if(!E(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data)))
- RE(reset_start_end(view_data))
-
- /* Default plain fill colour is previous value */
- fill_colour = reset_colour;
- char validation[16];
- sprintf(validation, "r2;C/%X", (palette[fill_colour] & 0xffffff00) >> 8);
- RE(button_set_validation(0, id_block->self_id, INSERT_PLAINCOL, validation))
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _Insert_colourshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Colour selected by picker */
- Pal256ColourSelectedEvent *pcse = (Pal256ColourSelectedEvent *)event;
- ObjectId parent;
- ComponentId parent_component, button;
- ViewData *view_data;
-
- E_RETV(toolbox_get_parent(0, id_block->self_id, &parent, &parent_component), 0)
- if(parent != Insert_sharedid)
- return 0; /* event not handled */
-
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
-
- /* Record new colour */
- switch (parent_component) {
- case INSERT_PLAINPOPUP:
- if(fill_colour == pcse->colour_number)
- return 1; /* no change */
- fill_colour = pcse->colour_number;
- button = INSERT_PLAINCOL;
- break;
-
- case INSERT_STARTPOPUP:
- if(start_colour == pcse->colour_number)
- return 1; /* no change */
- start_colour = pcse->colour_number;
-
- /* Should we select the 'include start colour' option? */
- if(view_data->caret_position <= 0)
- /* no preceding colour */
- RE(optionbutton_set_state(0, id_block->parent_id, INSERT_INCSTART, 1))
- else {
- RE(optionbutton_set_state(0, id_block->parent_id, INSERT_INCSTART,
- start_colour != get_shade(&view_data->sky, view_data->caret_position-1)))
- }
- button = INSERT_STARTCOL;
- break;
-
- case INSERT_ENDPOPUP:
- if(end_colour == pcse->colour_number)
- return 1; /* no change */
- end_colour = pcse->colour_number;
-
- /* Should we select the 'include end colour' option? */
- {
- int fol_colour;
- if(view_data->caret_position > 62)
- fol_colour = 0; /* no following colour, so use black */
- else
- fol_colour = get_shade(&view_data->sky, view_data->caret_position);
- RE(optionbutton_set_state(0, id_block->parent_id, INSERT_INCEND,
- end_colour != fol_colour))
- }
- button = INSERT_ENDCOL;
- break;
-
- default:
- return 0; /* eh?! */
- }
-
- /* Display new colour */
- char validation[16];
- sprintf(validation, "r2;C/%X", (palette[pcse->colour_number] & 0xffffff00) >> 8);
- RE(button_set_validation(0, parent, button, validation))
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _Insert_radiohandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- RadioButtonStateChangedEvent *rbsce = (RadioButtonStateChangedEvent *)event;
-
- switch(id_block->self_component) {
- case INSERT_RADPLAIN:
- update_plain_fill(rbsce->state);
- return 1; /* claim event */
-
- case INSERT_RADGRAD:
- update_grad_fill(rbsce->state);
- return 1; /* claim event */
-
- default:
- return 0; /* unknown button */
- }
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _Insert_popuphandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Colour picker about to pop up - set colour */
- switch(id_block->self_component) {
- case INSERT_PLAINPOPUP:
- RE(Pal256_set_colour(((PopUpAboutToBeShownEvent *)event)->menu_id, fill_colour))
- return 1; /* claim event */
-
- case INSERT_STARTPOPUP:
- RE(Pal256_set_colour(((PopUpAboutToBeShownEvent *)event)->menu_id, start_colour))
- return 1; /* claim event */
-
- case INSERT_ENDPOPUP:
- RE(Pal256_set_colour(((PopUpAboutToBeShownEvent *)event)->menu_id, end_colour))
- return 1; /* claim event */
-
- default:
- return 0; /* event not handled */
- }
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _Insert_buttonhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event;
- ViewData *view_data;
-
- switch(id_block->self_component) {
- case INSERT_GO:
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
-
- E_RETV(numberrange_get_value(0, id_block->self_id, INSERT_NUMTOINS, &number), 1)
-
- /* Stupidity checks */
- if(view_data->selection_exists
- || number < 1
- || view_data->caret_position > 62) {
- _kernel_oswrch(7); /* warning beep */
- return 1; /* claim event */
- }
-
- E_RETV(radiobutton_get_state(0, id_block->self_id, INSERT_RADPLAIN, NULL, &radio_sel), 1)
- switch(radio_sel) {
- case INSERT_RADGRAD: {
- int include_start, include_end;
- RE(optionbutton_get_state(0, id_block->self_id, INSERT_INCSTART, &include_start))
- RE(optionbutton_get_state(0, id_block->self_id, INSERT_INCEND, &include_end))
- gradient_insert(&view_data->sky, view_data->caret_position, number, start_colour, end_colour, include_start|(include_end<<1));
- } break;
-
- case INSERT_RADPLAIN:
- plain_insert(&view_data->sky, view_data->caret_position, number, fill_colour);
- reset_colour = fill_colour;
- break;
-
- default:
- return 1; /* unknown operation */
- }
-
- EditSky_rowsupdated(view_data, view_data->caret_position, 62);
- EditSky_markaschanged(view_data);
-
- /* Move cursor after newly inserted rows */
- if(view_data->caret_position + number <= 63)
- EditSky_set_caretpos(view_data, view_data->caret_position + number);
- else
- EditSky_set_caretpos(view_data, 63);
-
- if(FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust))
- RE(reset_start_end(view_data)) /* Update dbox */
-
- return 1; /* claim event */
-
- case INSERT_CANCEL:
- if(FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust)) {
- /* Reset dbox state */
- _Insert_showhandler(Window_AboutToBeShown, NULL, id_block, handle);
- RE(numberrange_set_value(0, id_block->self_id, INSERT_NUMTOINS, number))
- RE(radiobutton_set_state(0, id_block->self_id, radio_sel, 1))
- update_grad_fill(radio_sel == INSERT_RADGRAD);
- update_plain_fill(radio_sel == INSERT_RADPLAIN);
- }
-
- return 1; /* claim event */
-
- default:
- return 0; /* unknown button */
- }
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void update_grad_fill(bool sel)
- {
- /* Enable/disable the gradated fill controls */
- RE(setgadgetfaded(Insert_sharedid, INSERT_ENDLAB, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_ENDCOL, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_ENDPOPUP, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_INCEND, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_STARTLAB, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_STARTCOL, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_STARTPOPUP, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_INCSTART, !sel))
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void update_plain_fill(bool sel)
- {
- RE(setgadgetfaded(Insert_sharedid, INSERT_PLAINLAB, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_PLAINCOL, !sel))
- RE(setgadgetfaded(Insert_sharedid, INSERT_PLAINPOPUP, !sel))
- }
-
- /* ----------------------------------------------------------------------- */
-
- static _kernel_oserror *reset_start_end(ViewData *view_data)
- {
- char validation[16];
-
- /* Default end colour is colour above cursor */
- if(view_data->caret_position > 62)
- end_colour = 0; /* no following colour, so use black */
- else
- end_colour = get_shade(&view_data->sky, view_data->caret_position);
- sprintf(validation, "r2;C/%X", (palette[end_colour] & 0xffffff00) >> 8);
- THROW(button_set_validation(0, Insert_sharedid, INSERT_ENDCOL, validation))
-
- /* Default is not to include the end colour */
- THROW(optionbutton_set_state(0, Insert_sharedid, INSERT_INCEND, 0))
-
- /* Default start colour is colour below the cursor */
- if(view_data->caret_position <= 0)
- start_colour = 255; /* no previous colour, so use white */
- else
- start_colour = get_shade(&view_data->sky, view_data->caret_position-1);
- sprintf(validation, "r2;C/%X", (palette[start_colour] & 0xffffff00) >> 8);
- THROW(button_set_validation(0, Insert_sharedid, INSERT_STARTCOL, validation))
-
- /* Default is to include the start colour if we are at the bottom */
- return optionbutton_set_state(0, Insert_sharedid, INSERT_INCSTART, view_data->caret_position <= 0);
- }
-